package com.chookss.tiku;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.XJson;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FeedbackActivity extends BaseAct {
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.common_right_txt)
    TextView commonRightTxt;
    @BindView(R.id.tvTiltle)
    TextView tvTiltle;
    @BindView(R.id.edContent)
    EditText edContent;
    private int type = 0;//0题目纠错 1  新闻工作台审核未通过  2面试评价  3图文工作台未通过
    private String subjectCode,personCode;
    private int pass=0;//0不通过1通过
    private List<Map<String, String>> myWorkBeans; //未通过的数据
    private int maxSize = 500;//最大字数
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);

    }

    @Override
    public void initData() {
        initState();
        type = getIntent().getIntExtra("type", 0);
        subjectCode = getIntent().getStringExtra("subjectCode");
        //面试评价所用
        pass = getIntent().getIntExtra("pass", 0);
        personCode = getIntent().getStringExtra("personCode");
        myWorkBeans = (List<Map<String, String>>) getIntent().getSerializableExtra("myWorkBeans");
        commonRightTxt.setText("提交");
        commonRightTxt.setTextColor(getResources().getColor(R.color.main_color));
        commonRightTxt.setVisibility(View.VISIBLE);
        if (type == 0) {
            commonTitleTxt.setText("纠错");
            tvTiltle.setText("错误描述");
            edContent.setHint("请输入原因");
            tvTiltle.setVisibility(View.VISIBLE);
        } else if (type ==1||type ==3) {
            commonTitleTxt.setText("审核");
            edContent.setHint("请输入不通过原因");
        }else if (type == 2) {
            commonTitleTxt.setText("评价");
            edContent.setHint("请输入评价内容");
        }
        edContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > maxSize-1) {
                    MyToast.show("最多只能输入"+maxSize+"个字");
                }
            }
        });
    }

    private void getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("subjectCode", subjectCode);
        map.put("errorDecs", edContent.getText().toString());
        showLoading();
//        MyHttpRequest.getRequest(Urls.saveErrorCorrection, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
//            @Override
//            public void onSuccess(Response<String> response) {
//                dismissLoading();
//                MyToast.show("提交成功");
//                finish();
//            }
//        });
        MyHttpRequest.postRequestJson(Urls.saveErrorCorrection, this, XJson.mapToJObj(map).toString(), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                dismissLoading();
                MyToast.show("提交成功");
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        MyToast.show("提交成功");
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    MyToast.show("操作失败，请稍后重试");
                }

            }
        });
    }

    private void noPassData() {
        Map<String, Object> map = new HashMap<>();
        //1审核通过 2审核未通过0未审核
        for (int i = 0; i < myWorkBeans.size(); i++) {
            myWorkBeans.get(i).put("auditStatus", "2");
            myWorkBeans.get(i).put("noPassReason", edContent.getText().toString());
            myWorkBeans.get(i).put("remark", edContent.getText().toString());
        }
        map.put("tbKmerWorkFlowProjects", XJson.listToJsArrays(myWorkBeans));
        MyHttpRequest.postRequestJson(Urls.auditWorkfolwProject, this, XJson.mapToJObj(map).toString(), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        MyToast.show("提交成功");
                        EventBus.getDefault().post(new MyEvent("RefreshWork"));
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    MyToast.show("操作失败，请稍后重试");
                }
            }
        });
    }

    private void updateInterView() {
        Map<String, String> map = new HashMap<>();
        map.put("pass", pass+"");
        map.put("personCode",personCode);
        map.put("interviewEvaluate", edContent.getText().toString());
        MyHttpRequest.postRequest(Urls.updateInterView, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    dismissLoading();
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        EventBus.getDefault().post(new MyEvent("updateInterView_result"));
                        MyToast.show("评价成功");
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    MyToast.show("评价提交失败，请稍后重试");
                }
                finish();
            }
        });
    }

    @OnClick({R.id.common_title_back, R.id.common_right_txt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.common_right_txt:
                if(edContent==null){
                    return;
                }
                if (Urls.antiShake.check()) {
                    return;
                }
                if (Utils.isNull(edContent.getText().toString())) {
                    MyToast.show("请输入原因");
                } else if (type == 0) {
                    getData();
                } else if (type ==1||type ==3){
                    noPassData();
                } else if (type == 2){
                    updateInterView();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (edContent != null) {
            Utils.hideInputKeyboard(this, edContent);
        }
    }
}
